<template>
  <page-container title="司机管理">
    <template #extra>
      <el-button @click="onAddDriver">添加司机</el-button>
    </template>
	<div class="mt-4">
	  <el-input
	    v-model="query"
	    style="max-width: 600px"
	    placeholder="请输入查询信息"
	    class="input-with-select"
	    clearable
	    @clear="clear"
	  >
	    <template #append>
	      <el-button :icon="Search" @click="findByQuery" />
	    </template>
	  </el-input>
	</div>
    <!-- v-loading="loading"这个是页面加载的loading效果 -->
    <el-table v-loading="loading" style="width: 100%" :data="DriverList">
      <el-table-column label="司机ID" width="150" type="index"></el-table-column>
      <el-table-column label="司机名称" prop="driverName"></el-table-column>
      <el-table-column label="出生日期" prop="birthday"></el-table-column>
      <el-table-column label="家庭住址" prop="address"></el-table-column>
	  <el-table-column label="联系方式" prop="cell"></el-table-column>
	 <el-table-column label="工作表" prop="schedules">
		  <template #default="scope">
			  <div v-for="item in scope.row.schedules" :key="item.id">
				  <span prop="startTime">{{item.start_time}} - {{item.end_time}}</span>
			  </div>
		  </template>
	  </el-table-column>
      <el-table-column label="操作">
        <template #default="{ row, $index }">
          <el-button
            :icon="Edit"
            circle
            type="primary"
            plain
            @click="handleEdit(row, $index)"
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            type="danger"
            plain
            @click="onDelDriver(row, $index)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据"></el-empty>
      </template>
    </el-table>
    <!-- 添加分类弹框 -->
    <DriverEdit ref="dialog" @success="onSuccess"></DriverEdit>
  </page-container>
</template>

<script setup>
import { ref } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import { DelDriverService, GetDriverService,GetDriverFindByQueryService } from '@/api/Driver'
import DriverEdit from './components/DriverEdit.vue'
import { Search } from '@element-plus/icons-vue'

const dialog = ref()
const DriverList = ref([])
// loading加载页面效果
const loading = ref(false)

const query = ref('')
const findByQuery = async () => {
  loading.value = true
  console.log(query.value)
  const res = await GetDriverFindByQueryService(query.value)
  if (res.data.meta.status !== 200) {
    ElMessage.error(res.data.meta.msg)
    return
  }
  DriverList.value = res.data.data
  loading.value = false
}

const clear = () => {
  query.value = ''
  getDriverList()
}

const getDriverList = async () => {
  loading.value = true
  const res = await GetDriverService()
  console.log(res);
  if (res.data.meta.status !== 200) {
    ElMessage.error(res.data.meta.msg)
    return
  }
  DriverList.value = res.data.data
  loading.value = false
}

getDriverList()

//点击添加分类跳出弹框
const onAddDriver = () => {
  // dialogFormVisible.value = true
  dialog.value.open({})
}
//点击编辑的时候跳出弹框
const handleEdit = (row) => {
  dialog.value.open(row)
}

//点击删除,删除所在列表
const onDelDriver = async (row) => {
  await ElMessageBox.confirm('你确认要删除该司机么', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await DelDriverService(row.id)
  ElMessage.success('删除成功')
  getDriverList()
}

const onSuccess = () => {
  getDriverList()
}
</script>
